package cn.tedu.d20_reflection.work;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
 1.1 遍历输出Emp的属性名和方法名
 1.2 通过反射调用test02(String name,double score),方法
 1.3 Emp e=new Emp("张三",5000.0)
     通过反射克隆一个e对象
 */

public class Test {

    @org.junit.Test
    public void method1() {
        //获取字节码对象
        Class<?> clazz = Emp.class;

        //1. 遍历输出
        Method[] m = clazz.getMethods();
        Field[] f = clazz.getDeclaredFields();
        for (Method m1 : m) {
            System.out.println(m1.getName());
        }
        System.out.println("------------------");
        for (Field f2 : f) {
            System.out.println(f2.getName());
        }
    }

    //2. 通过反射调用test02(String name,double score),方法
    @org.junit.Test
    public void method2() throws Exception {
        Class<?> clazz = Emp.class;
        Method method = clazz.getDeclaredMethod("test02", String.class, double.class);//, String.class, double.class
        method.invoke(clazz.newInstance(), "孙悟空", 500.0);//,"孙悟空",500.0


    }
    @org.junit.Test
    public void method3() throws Exception {
        Class<?> clazz = Emp.class;
        //3. Emp e=new Emp("张三",5000.0) 通过反射克隆一个e对象
        Constructor<?> constructor = clazz.getConstructor(String.class, double.class);
        Object o1 = constructor.newInstance("张三", 5000.0);
        Emp e = (Emp) o1;
        System.out.println(e);
    }





}
